package com.controls.ws;

import java.io.IOException;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.content.Context;
import android.util.Log;

import com.Utility.WebServiceCommunicator;
import com.events.WebServiceCommunicatorListener;

public class BaseWSControl {
	private Context mContext;
	private WebServiceCommunicatorListener mWCListener;
	private WebServiceCommunicator mWComunicator;
	/*
	 * 
	 * constructor
	 */
	public BaseWSControl(Context cxt, WebServiceCommunicatorListener lsn) {
		this.mContext = cxt;
		this.mWCListener = lsn;
	}
	/**
	 * 
	 */
	public void cancel(){
		if (mWComunicator != null) {
			mWComunicator.cancel(true);
			mWComunicator.setWebServiceCommuncatorListener(null);
		}
	}
	/**
	 * 
	 * @param flag
	 * @param content
	 */
	public void fetch(WebServiceCommunicator.WebServiceFlag flag,String content){
		cancel();
		mWComunicator = new WebServiceCommunicator(mContext);
		mWComunicator.setWebServiceCommuncatorListener(mWCListener);
		mWComunicator.fetch(flag, content);
	}
	/**
	 * 
	 * @param xml
	 * @return
	 */
	protected Document getDomElement(String xml) {
		Document doc = null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {

			DocumentBuilder db = dbf.newDocumentBuilder();

			InputSource is = new InputSource();
			is.setCharacterStream(new StringReader(xml));
			doc = db.parse(is);

		} catch (ParserConfigurationException e) {
			Log.e("Error: ", e.getMessage());
			return null;
		} catch (SAXException e) {
			Log.e("Error: ", e.getMessage());
			return null;
		} catch (IOException e) {
			Log.e("Error: ", e.getMessage());
			return null;
		}
		// return DOM
		return doc;
	}

}
